<style scoped>
    .layui-row {
        overflow: hidden;
    }
    .select-item {
        width: 34.333333%;
        float: right;
    }
    .layui-row:after, .layui-row:before {
        clear: none;
    }
    .layui-form >>> .layui-form-select{
        z-index: inherit;
    }
</style>

<template>
    <div class="department">
<!--        <div style="height:50px">-->
<!--            <a href="javascript:;" class="layui-btn" @click="add"><i class="layui-icon">&#xe654;</i>导出excel</a>-->
<!--        </div>-->
        <form class="layui-form " >
            <div  class="layui-col-lg4 select-item" style="text-align: right;width: auto" >

                <div class="layui-inline" style="width: 200px">
                    <input type="text" v-model="starttime" id="starttime" autocomplete="off" placeholder="请选择开始时间" class="layui-input key">

                </div>
                <div class="layui-inline" style="width: 200px">
                    <input type="text" v-model="endtime" id="endtime" autocomplete="off"  placeholder="请选择结束时间" class="layui-input key">

                </div>
                <div class="layui-inline" style="width: 200px">

                    <select   v-model="srguid" name="srguid"  id="srguid"  lay-filter="storeroom"       placeholder="请选择库房"   class="layui-form-select"   >
                    </select>
                </div>

                <button type="button" class="layui-btn sou" @click="search">搜索</button>
                <button type="button" class="layui-btn sou" @click="exportExcel">导出excel</button>
            </div>

        </form>
        <table class="layui-table layui-form"   style="height:200px">

            <thead>
            <tr>
                <th>销售单编号 </th>
                <th>订单编号</th>
                <th>出库日期</th>
                <th>总价</th>
                <th>产品</th>
                <th>规格</th>
                <th>重量（吨）</th>
                <th>单价</th>
            </tr>
            </thead>
            <tbody>
            <template v-for="(item) in whoutform" >
                <tr >
                    <td>{{item.code}}</td>
                    <td>{{item.scode}}</td>
                    <td>{{item.createtime}}</td>
                    <td>{{item.amount}}</td>
                    <td>{{item.product}}</td>
                    <td>{{item.spname}}</td>
                    <td>{{item.weight}}</td>
                    <td>{{item.iprice}}</td>
                </tr>
            </template>
            </tbody>
        </table>

        <Pagination :cur="currentPage" :all="totalPage" :callback="turnPage" ></Pagination>
        <div style="height: 200px"></div>
    </div>
</template>

<script>
    import Pagination from '../common/Pagination.vue'
    export default {
        name: 'Exporywhoutform',
        components: {Pagination},
        data () {
            return {
                departments: [],
                currentPage: 1,
                rows: 10,
                totalPage: 0,
                total: 1000,
                searchName: '',
                afterList: [],
                storehouseroom:[],
                storeroom:[],
                starttime:"",
                endtime:"",
                srguid:"",
                shrguid:"",
                whoutform:[],
                exportpage:1
            }
        },
        mounted() {
            let that = this;
            this.getBaseInfo();


            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;
             form.on('select(storeroom)', function(data){
                    $.each(that.storehouseroom, function (index, item) {

                        if(item.storeguid == data.value){
                            $('#shrguid').append(new Option(item.name , item.guid));// 下拉菜单里添加元素。
                            layui.form.render("select");
                        }
                    });
             })
            });
            layui.use('laydate', function(){
                var laydate = layui.laydate;

                //执行一个laydate实例
                laydate.render({
                    elem: '#starttime' //指定元素
                });
                //执行一个laydate实例
                laydate.render({
                    elem: '#endtime' //指定元素
                });
            });
        },
        methods: {
            // 数据转换
            tansList () {
                this.afterList = Object.values(this.departments.reduce((temp, item) => {
                    if( temp[item.telphone] ) {
                        temp[item.telphone].push(item)
                    }else {
                        temp[item.telphone]  = [item]
                    }
                    return temp
                },{}))
            },
            search: function() {
                this.getPageData('search');
                console.log(this);
            },
            turnPage: function(data){
                this.currentPage = data;
                this.exportpage = data;
                this.getPageData();
            },
            getPageData: function(type = '') {
                var that = this;

                let loader = layui.layer.load();

                let postData = {rows: that.rows};
                if (type != 'search') {
                    postData.page = that.currentPage;
                }
                that.starttime = $('#starttime').val();
                that.endtime = $('#endtime').val();
                that.srguid =  $('#srguid').val();
                that.shrguid =  $('#shrguid').val();

                postData.starttime = that.starttime;
                postData.endtime = that.endtime;
                postData.srguid = that.srguid;
                postData.shrguid = that.shrguid;
                $.ajax({
                    url: '/sales/Salesorder!findDetailPC.action',
                    type: "post",
                    data: postData,
                    dataType: "json",
                    success: function(res) {

                        if (res.info != undefined && res.info.rows != undefined) {
                            //分页数据赋值
                            that.currentPage = res.info.indexPage;
                            that.totalPage = res.info.pageCount;
                            that.whoutform = res.info.rows;
                            that.total =  res.info.total;
                            that.tansList()

                        } else {
                            that.alert("系统错误");
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });
            },getBaseInfo: function(type = '') {
                var that = this;
                $.ajax({
                    url: '/sales/Whoutform!getBaseInfo.action?openid=' + localStorage.openid,
                    type: "post",
                    dataType: "json",
                    success: function(res) {
                        console.log(res)
                        if (res.msg == 'success') {
                            let data =res;
                            that.storeroom = data.storeroom;
                            that.storehouseroom = data.storehouseroom;
                            $('#srguid').append(new Option('请选择库房', ""));// 下拉菜单里添加元素。
                            $('#shrguid').append(new Option('请选择库间', ""));// 下拉菜单里添加元素。
                            $.each(data.storeroom, function (index, item) {
                                $('#srguid').append(new Option(item.name , item.guid));// 下拉菜单里添加元素
                            });
                            layui.form.render("select");
                        } else {
                            $.toast("系统错误", 'forbidden');
                        }
                    },
                    complete: function () {
                        layer.close();
                    }
                });
            },
            compare: function (property){
                return function(a,b){
                    var value1 = a[property];
                    var value2 = b[property];
                    return value1 - value2;
                }
            },
            add: function () {
                this.$parent.switchPage('addSalesPermission');
            },
            exportExcel:function (){
                var that = this;
                let postData = {rows: that.rows};
                that.starttime = $('#starttime').val();
                that.endtime = $('#endtime').val();
                that.srguid =  $('#srguid').val();
                that.shrguid =  $('#shrguid').val();

                window.location.href="/sales/Salesorder!exportOrderData.action?starttime=" + that.starttime + "&endtime=" + that.endtime + "&srguid=" + that.srguid + "&rows=" + that.total +  "&page=1";
            },
            edit: function (id) {
                this.$parent.gid = id;
                this.$parent.switchPage('EditDepartment');
            },
            del: function (id) {
                let that = this;
                layer.confirm('确定要删除该数据吗？', function () {
                    let loader = layer.load();
                    $.ajax({
                        url: '/sales/Salespermission!del.action',
                        type: "post",
                        data: {guid: id},
                        dataType: "json",
                        success: function(res) {
                            if (res.info == "ok") {
                                that.toast("删除成功");
                                that.getPageData('search');
                            } else {
                                that.toast("系统错误");
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                })
            }
        }
    }
</script>
